import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '@/views/Main.vue'
import Login from '@/views/Login.vue'
import CategoryEdit from '@/views/category/CategoryEdit.vue'
import CategoryList from '@/views/category/CategoryList.vue'
import ItemEdit from '@/views/item/ItemEdit.vue'
import ItemList from '@/views/item/ItemList.vue'
import HeroEdit from '@/views/hero/HeroEdit.vue'
import HeroList from '@/views/hero/HeroList.vue'
import ArticleEdit from '@/views/article/ArticleEdit.vue'
import ArticleList from '@/views/article/ArticleList.vue'
import AdEdit from '@/views/ad/AdEdit.vue'
import AdList from '@/views/ad/AdList.vue'
import AdminUserEdit from '@/views/admin_user/AdminUserEdit.vue'
import AdminUserList from '@/views/admin_user/AdminUserList.vue'
import GreatVideoEdit from '@/views/great_video/GreatVideoEdit.vue'
import GreatVideoList from '@/views/great_video/GreatVideoList.vue'

Vue.use(VueRouter)

const routes = [
  // meta:{isPublic: true } 添加一个参数来表示可以公开访问 用来设置登录限制
  {path:'/login',name:'login',component:Login,meta:{isPublic: true }},
  {
    path: '/',
    name: 'Main',
    component: Main,
    children:[
      {path:'/categories/create',component:CategoryEdit},
      // 编辑时跳转路由(props: true表示把左边的任何url参数【例如id】都注入到 CategoryEdit页面)
      {path:'/categories/edit/:id', component:CategoryEdit, props: true},
      {path:'/categories/list',component:CategoryList},

      {path:'/items/create',component:ItemEdit},
      {path:'/items/edit/:id', component:ItemEdit, props: true},
      {path:'/items/list',component:ItemList},

      {path:'/heroes/create',component:HeroEdit},
      {path:'/heroes/edit/:id', component:HeroEdit, props: true},
      {path:'/heroes/list',component:HeroList},

      {path:'/articles/create',component:ArticleEdit},
      {path:'/articles/edit/:id', component:ArticleEdit, props: true},
      {path:'/articles/list',component:ArticleList},

      {path:'/ads/create',component:AdEdit},
      {path:'/ads/edit/:id', component:AdEdit, props: true},
      {path:'/ads/list',component:AdList},

      {path:'/admin_users/create',component:AdminUserEdit},
      {path:'/admin_users/edit/:id', component:AdminUserEdit, props: true},
      {path:'/admin_users/list',component:AdminUserList},

      {path:'/great_videos/create',component:GreatVideoEdit},
      {path:'/great_videos/edit/:id', component:GreatVideoEdit, props: true},
      {path:'/great_videos/list',component:GreatVideoList},
    ]
  },
]

const router = new VueRouter({
  routes
})

// 定义导航前置守卫(客服端路由限制)
router.beforeEach((to,form,next) => {
  // to  即将要进入的目标 路由对象
  // 客服端路由限制 -->如果没有公开访问参数isPublic和token 则进行页面跳转
  if(!to.meta.isPublic && !localStorage.token){
    // next('/') 或者 next({ path: '/' }): 跳转到一个不同的地址。
    return next('/login')
  }
  // next(): 进行管道中的下一个钩子
  next()
})
export default router
